#!/bin/bash
################################################################################
#    Ogame.pm Perl Ogame BOT
#    Copyright (C) 2008  <ogame.pm@gmail.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

#exemple
#./auto_attaque_inactifs toto55 1-0

cd ~ogame

case "$1" in
   toto55)
      armes=5;bouclier=6;protection=7;univers=toto55;nb_sonde=1;max_slots=4;fleet=" -sonde 1 "
      case "$2" in
         1-0)
            planetid=0;galaxy=1;systeme=101;nb_scan=20;delai=140
         ;;
         1-1)
            planetid=0;galaxy=1;systeme=121;nb_scan=20;delai=140
         ;;
         2-0)
            planetid=4;galaxy=2;systeme=99;nb_scan=20;delai=140
         ;;
         2-1)
            planetid=4;galaxy=2;systeme=119;nb_scan=20;delai=140
         ;;
         *)
            echo "Arg 2 missing"
            exit 1
         ;;
      esac
      ;;

    *)
        echo "Usage: $0 uni attack_id" >&2
        exit 3
        ;;
esac

echo " "
echo "----------------------------- "
echo "Begin auto attaque univers $1 --> " `date +'%d/%m  %H':'%M':'%S'`
mkdir $1 > /dev/null 2>&1
./ogbot.pl -s $1 -a check

rm $1/*
echo "Begin delete messages initiaux $1 --> " `date +'%d/%m  %H':'%M':'%S'`
sqlite3 ~ogame/$1.db < ~ogame/histo.sql
sqlite3 ~ogame/$1.db "delete from messages where type='Espionnage';"
sqlite3 ~ogame/$1.db "delete from espionnage;"

echo "Begin scan pour $1 galaxie $galaxy systeme $systeme nombre de system: $nb_scan  --> " `date +'%d-%m  %H':'%M':'%S'`
./ogbot.pl -s $1 -a scan --galaxie=$galaxy --systeme=$systeme --nscan=$nb_scan --planetid=$planetid > $1/scan.txt
egrep '\([ iI]+\)' $1/scan.txt |grep -v plan > $1/i.txt
echo "End scan pour $1 --> " `date +'%d-%m %H':'%M':'%S'`
echo " "

./ogbot.pl -s $1 -a get_messages
echo "End delete messages initiaux $1 --> " `date +'%d/%m  %H':'%M':'%S'`
echo " "

#DATE=`date +'%m-%d'`
DATE='01-01'
TIME=`date +'%H':'%M':'%S'`
echo "DATE: " $DATE "TIME: " $TIME

echo "Begin envoi sondes $1 --> " `date +'%d-%m %H':'%M':'%S'`
./ogbot.pl -s $1 -a multi_sonde -fichier $1/i.txt -sonde $nb_sonde -o $planetid -max $max_slots -delai $delai
./ogbot.pl -S $1 -A check
sleep $delai
echo "End envoi sondes $1 --> " `date +'%d-%m %H':'%M':'%S'`
echo " "

echo "Begin lecture messages univers $1 --> " `date +'%d-%m %H':'%M':'%S'`
./ogbot.pl -s $1 -a get_messages
echo "End lecture messages univers $1 --> " `date +'%d/%m  %H':'%M':'%S'`
echo " "

echo "Begin calcul univers $1 --> " `date +'%d-%m %H':'%M':'%S'`
echo "~ogame/calc_func.pl -armes $armes -bouclier $bouclier -protection $protection -date 01-01 -db ~/ogame/$1.db $fleet > $1/cibles.txt"
~ogame/calc_func.pl -armes $armes -bouclier $bouclier -protection $protection -date $DATE -time $TIME -db ~ogame/$1.db $fleet > $1/cibles.txt
echo "End lecture calcul univers $1 --> " `date +'%d/%m  %H':'%M':'%S'`
echo " "

#on trie par ordre d'interet
sort -r -n $1/cibles.txt|head  -$max_slots > $1/cibles-$galaxy.txt
echo "Begin lancement attaques univers $1 --> " `date +'%d/%m  %H':'%M':'%S'`

#tail -$max_slots $univers/cibles-$galaxy.txt|awk '{system("./ogbot.pl -s " "'$univers'" " -a attaque  -gt "$1 "'"$fleet"'" -d $2 -o "'$planetid'")}'
awk '{system("./ogbot.pl -s "  "'$1'"  "'"$fleet"'" "-a attaque  -pt "$1" -d " $2 " -o ""'"$planetid"'")}' $1/cibles-$galaxy.txt

echo "End univers $1 --> " `date +'%d/%m  %H':'%M':'%S'`
echo " "
echo "----------------------------- "
